package com.xhp.xtool.controller;

import com.xhp.xtool.domain.*;
import com.xhp.xtool.util.ftp.FtpUtil;
import com.xhp.xtool.util.redis.JedisUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * 主页
 *
 * @Description
 * @Author xhp
 * @Date 2023/2/8 15:29
 **/
@RestController
@RequestMapping("/index")
public class IndexController {

    /**
     * FTP连接测试
     *
     * @param ftp
     * @return
     */
    @PostMapping("ftpTest")
    public AjaxResult ftpTest(FtpDto ftp) {
        try {
            int state = FtpUtil.getState(FtpUtil.getFtpPool(ftp));
            return state == 200 ? AjaxResult.success() : AjaxResult.error("连接失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("错误：" + e.toString());
        }
    }

    /**
     * FTP获取文件
     *
     * @param ftp
     * @return
     */
    @PostMapping("ftpFiles")
    public AjaxResult ftpFiles(FtpDto ftp) {
        try {
            List<FileVo> fileVoList = FtpUtil.getFiles(FtpUtil.getFtpPool());
            return AjaxResult.success(fileVoList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("错误：" + e.toString());
        }
    }

    /**
     * 上传测试文件
     *
     * @return
     */
    @PostMapping("ftpUpload")
    public AjaxResult ftpUpload() {
        try {
            //读取测试文件
            Resource resource = new ClassPathResource("file/test.txt");
            // 获取临时目录
            String property = "java.io.tmpdir";
            String tempDir = System.getProperty(property);
            //将测试文件写入临时文件
            File tempFile = new File(tempDir + "/test.txt");
            try (InputStream inputStream = resource.getInputStream();) {
                FileUtils.copyInputStreamToFile(inputStream, tempFile);
            }

            FtpUtil.upload(FtpUtil.getFtpPool(), tempFile);
            //删除临时文件
            tempFile.delete();
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("错误：" + e.toString());
        }
    }

    /**
     * 删除测试文件
     *
     * @return
     */
    @PostMapping("ftpDelete")
    public AjaxResult ftpDelete() {
        try {
            FtpUtil.deleteFiles(FtpUtil.getFtpPool(), "test.txt");
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("错误：" + e.toString());
        }
    }

    /**
     * Redis连接测试
     *
     * @param redisDto
     * @return
     */
    @PostMapping("redisTest")
    public AjaxResult ftpTest(RedisDto redisDto) {
        try {
            return AjaxResult.success(JedisUtil.testConnect(redisDto));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("错误：" + e.toString());
        }
    }

    /**
     * Redis获取keys
     *
     * @return
     */
    @PostMapping("redisKeys")
    public AjaxResult redisKeys(Integer index) {
        try {
            return AjaxResult.success(JedisUtil.keys(index));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("错误：" + e.toString());
        }
    }

    /**
     * Redis获取key的值
     *
     * @return
     */
    @PostMapping("redisValue")
    public AjaxResult redisValue(String key, Integer index) {
        try {
            String type = JedisUtil.type(key, index);
            List<RedisVo> redisVoList = JedisUtil.getValue(key, type, index);
            return AjaxResult.success(new RedisResult(redisVoList, type));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("错误：" + e.toString());
        }
    }
}
